package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryPlfAuthReq;
import com.sg.dto.base.framework.req.BatchQueryPltRoleAuthListReq;
import com.sg.dto.base.framework.req.QueryPlfAuthDetailReq;
import com.sg.dto.base.framework.req.QueryPlfAuthListReq;
import com.sg.entity.PlatformAuthorize;
import com.sg.service.base.framework.MPlatformAuthorizeService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.PlatformAuthorizeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MPlatformAuthorizeServiceImpl
        extends ServiceImpl<PlatformAuthorizeMapper, PlatformAuthorize>
        implements MPlatformAuthorizeService {

    @Autowired
    PlatformAuthorizeMapper platformAuthorizeMapper;

    /**
     * 1-1-06查询平台授权范围列表[1355]
     * gen by moon at 8/31/2022, 7:25:53 PM
     */
    @Trace(operationName = "1-1-06查询平台授权范围列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PlatformAuthorize> queryPlfAuthList(QueryPlfAuthListReq reqDto) {

        try {
            QueryWrapper<PlatformAuthorize> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PlatformAuthorize.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PlatformAuthorize.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PlatformAuthorize.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<PlatformAuthorize>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-06新增平台授权范围[1437]
     * gen by moon at 8/31/2022, 7:25:53 PM
     */
    @Trace(operationName = "1-1-06新增平台授权范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPlfAuth(PlatformAuthorize platformAuthorize) {
        platformAuthorize.setPlatformAuthorizeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        platformAuthorize.setIsDefault("FALSE");
        platformAuthorize.setIsArchive("FALSE");
        platformAuthorize.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        platformAuthorize.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (platformAuthorize != null && platformAuthorize.getSubjectLifeCycle() != null && platformAuthorize.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            platformAuthorize.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        platformAuthorize.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        platformAuthorize.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (platformAuthorize != null && platformAuthorize.getSubjectLifeCycle() != null && platformAuthorize.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            platformAuthorize.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        platformAuthorize.setIsValid("FALSE");
        platformAuthorize.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        platformAuthorize.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(platformAuthorize)) {
            return platformAuthorize.getPlatformAuthorizeId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-1-06批量查询角色授权列表[1602]
     * gen by moon at 8/31/2022, 7:25:56 PM
     */
    @Trace(operationName = "1-1-06批量查询角色授权列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PlatformAuthorize> batchQueryPltRoleAuthList(BatchQueryPltRoleAuthListReq reqDto) {

        try {
            QueryWrapper<PlatformAuthorize> queryWrapper = new QueryWrapper<>();
            reqDto.setTypeCode("ROLE");
            reqDto.setIsView("TRUE");
            reqDto.setAuthorizeTypeCode("SENCE");
            reqDto.setAuthorizedObject(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSceneId());//CURRENT_SCENEID
            if (reqDto.getPlfAuthList() == null || reqDto.getPlfAuthList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("authorized_content_id", reqDto.getPlfAuthList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PlatformAuthorize.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PlatformAuthorize.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PlatformAuthorize.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<PlatformAuthorize>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-06修改平台授权范围[1563]
     * gen by moon at 8/31/2022, 7:25:55 PM
     */
    @Trace(operationName = "1-1-06修改平台授权范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePlfAuth(PlatformAuthorize platformAuthorize) {
        platformAuthorize.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (platformAuthorize != null && platformAuthorize.getSubjectLifeCycle() != null && platformAuthorize.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            platformAuthorize.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (platformAuthorize != null && platformAuthorize.getSubjectLifeCycle() != null && platformAuthorize.getSubjectLifeCycle().equals("ARCHIVING")) {
            platformAuthorize.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        platformAuthorize.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (platformAuthorize != null && platformAuthorize.getSubjectLifeCycle() != null && platformAuthorize.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            platformAuthorize.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (platformAuthorize != null && platformAuthorize.getSubjectLifeCycle() != null && platformAuthorize.getSubjectLifeCycle().equals("ARCHIVING")) {
            platformAuthorize.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(platformAuthorize);
    }

    /**
     * 1-1-06批量删除平台授权范围[1551]
     * gen by moon at 8/31/2022, 7:25:54 PM
     */
    @Trace(operationName = "1-1-06批量删除平台授权范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePlfAuth(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-1-06批量修改平台授权范围[1550]
     * gen by moon at 8/31/2022, 7:25:54 PM
     */
    @Trace(operationName = "1-1-06批量修改平台授权范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdatePlfAuth(List<PlatformAuthorize> platformAuthorize) {

        if (platformAuthorize == null || platformAuthorize.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(platformAuthorize);

    }

    /**
     * 1-1-06查询平台授权范围详情[28]
     * gen by moon at 8/31/2022, 7:25:52 PM
     */
    @Trace(operationName = "1-1-06查询平台授权范围详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PlatformAuthorize queryPlfAuthDetail(QueryPlfAuthDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<PlatformAuthorize> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<PlatformAuthorize> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                PlatformAuthorize retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-06删除平台授权范围[27]
     * gen by moon at 8/31/2022, 7:25:52 PM
     */
    @Trace(operationName = "1-1-06删除平台授权范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePlfAuth(String platformAuthorizeId) {

        return super.removeById(platformAuthorizeId);
    }

    /**
     * 1-1-06批量查询平台授权范围[3452]
     * gen by moon at 8/31/2022, 7:25:56 PM
     */
    @Trace(operationName = "1-1-06批量查询平台授权范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PlatformAuthorize> batchQueryPlfAuth(BatchQueryPlfAuthReq reqDto) {

        try {
            QueryWrapper<PlatformAuthorize> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getPlfAuthList() == null || reqDto.getPlfAuthList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("authorized_content_id", reqDto.getPlfAuthList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PlatformAuthorize.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PlatformAuthorize.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PlatformAuthorize.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PlatformAuthorize>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
